package cn.com.sinux.spring.utils;

/**
 * Created by Administrator on 2016/7/13.
 */

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChartUtils {

    public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            ChartUtilities.writeChartAsPNG(out, chart, weight, height);
            out.flush();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    public static void main(String[] args){
        XYDataset xydataset = createXYDataset(10000);
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("工艺实验参数", "时间", "指数值", xydataset, true, true, true);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        dateaxis.setDateFormatOverride(new SimpleDateFormat("MM-dd HH:mm:ss"));
        dateaxis.setLabelFont(new Font("黑体",Font.BOLD,14));         //水平底部标题
        dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));  //垂直标题
        ValueAxis rangeAxis=xyplot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
        jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
        jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

        saveAsFile(jfreechart, "D:/test/a.png", 100000, 400);

//        ChartFrame chartFrame=new ChartFrame("某公司人员组织数据图",jfreechart);
//        //chart要放在Java容器组件中，ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的，不是正中间的标题。
//        chartFrame.pack(); //以合适的大小展现图形
//        chartFrame.setVisible(true);//图形是否可见

    }

    private static XYDataset createXYDataset(int l) {  //这个数据集有点多，但都不难理解
        TimeSeries timeseries =  new TimeSeries("温度");
        Date date = new Date();
        for(int i=0; i < l; i++){
            Date time = DateUtils.addSeconds(date, i);
            timeseries.add(new Second(time), Math.random() * 100);
        }
//        TimeSeries timeseries2 =  new TimeSeries("压强");
//        for(int i=0; i < l; i++){
//            Date time = DateUtils.addMinutes(date, i);
//            timeseries2.add(new Minute(time), Math.random() * 10);
//        }
//        TimeSeries timeseries3 =  new TimeSeries("压强");
//        for(int i=0; i < l; i++){
//            Date time = DateUtils.addMinutes(date, i);
//            timeseries3.add(new Minute(time), Math.random() * 5);
//        }
//        TimeSeries timeseries4 =  new TimeSeries("气压");
//        for(int i=0; i < l; i++){
//            Date time = DateUtils.addMinutes(date, i);
//            timeseries4.add(new Minute(time), Math.random() * 1000);
//        }
//        TimeSeries timeseries5 =  new TimeSeries("电流");
//        for(int i=0; i < l; i++){
//            Date time = DateUtils.addMinutes(date, i);
//            timeseries5.add(new Minute(time), Math.random() * 2);
//        }
//        TimeSeries timeseries6 =  new TimeSeries("维度");
//        for(int i=0; i < l; i++){
//            Date time = DateUtils.addMinutes(date, i);
//            timeseries6.add(new Minute(time), Math.random());
//        }
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        timeseriescollection.addSeries(timeseries);
//        timeseriescollection.addSeries(timeseries2);
//        timeseriescollection.addSeries(timeseries3);
//        timeseriescollection.addSeries(timeseries4);
//        timeseriescollection.addSeries(timeseries5);
//        timeseriescollection.addSeries(timeseries6);
        return timeseriescollection;
    }
}
